/**
 * Title: Hyperbolic engine
 * Description: jFamilyTree Engine
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.core;

/**
 * Encapsulation of some hyperbolic computations or conversions
 * 
 * @author Bernard Bou
 */
public class XMath
{
	// C O N V E R S I O N

	/**
	 * Map Euclidian distance to Hyperbolic space
	 * 
	 * @param e
	 *        Euclidian distance
	 * @return Hyperbolic distance
	 */
	static public double e2h(double e)
	{
		return 2. * atanh(e);
		// return (e+e) / (1.0 + e*e);
	}

	/**
	 * Map Hyperbolic distance to Euclidian space
	 * 
	 * @param h
	 *        Hyperbolic distance
	 * @return Euclidian distance
	 */
	static public double h2e(double h)
	{
		return tanh(h * 0.5);
		// return h / (1.0 + Math.sqrt( 1.0 - h*h ));
	}

	/**
	 * Map Euclidian space to Hyperbolic space
	 * 
	 * @param e
	 *        point in Euclidian space
	 * @return this point mapped to Hyperbolic space
	 */
	static public Complex e2h(Complex e)
	{
		e.set(e2h(e.re), e2h(e.im));
		return e;
	}

	/**
	 * Map Hyperbolic space to Euclidian space
	 * 
	 * @param h
	 *        point in Hyperbolic space
	 * @return this point mapped to Euclidian space
	 */
	static public Complex h2e(Complex h)
	{
		h.set(h2e(h.re), h2e(h.im));
		return h;
	}

	// H Y P E R B O L I C . T R I G O N O M E T R Y

	/**
	 * Hyperbolic tangence
	 * 
	 * @param x
	 *        input
	 * @return hyperbolic tangence of x
	 */
	static public double tanh(double x)
	{
		x = Math.exp(x);
		x *= x;
		return (x - 1) / (x + 1);
	}

	/**
	 * Hyperbolic arc tangence
	 * 
	 * @param x
	 *        input
	 * @return hyperbolic atan
	 */
	static public double atanh(double x)
	{
		return 0.5 * Math.log((1 + x) / (1 - x));
	}
}
